﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqModel;
using CDB.LinqData;

public partial class back_SendNewsLetter : System.Web.UI.Page
{

    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    NewsLetter news;
    Subscription subscription;
    List<NewsLetter> newss = new List<NewsLetter>();
    List<Subscription> subscriptions = new List<Subscription>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillNewsLetter();
        }
    }
    private void fillNewsLetter()
    {
        news = new NewsLetter(_dataObject);
        newss = news.GetAllNewsLetter();

        if (newss.Count > 0)
        {
            drpNewsletter.DataSource = newss;
            drpNewsletter.DataTextField = "Subject";
            drpNewsletter.DataValueField = "Id";
            drpNewsletter.DataBind();
        }

        drpNewsletter.Items.Insert(0, new ListItem("Please Select Newsletter", ""));
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        subscription = new Subscription(_dataObject);
        subscriptions = subscription.GetAllSubscriptions();

        // Note: Get the Newsletter to send.
        news = new NewsLetter(_dataObject);
        news.Id = Convert.ToInt32(drpNewsletter.SelectedValue);
        news.GetNewsLetterById();

        if (subscriptions.Count > 0)
        {
            // Note: Send Email.
            for (int i = 0; i < subscriptions.Count; i++)
                CDBGeneral.sendEmailToUsers(subscriptions[i].Email, news.Subject, news.Content, string.Empty);
        }

        drpNewsletter.SelectedIndex = 0;
        //messagegreen.Attributes.Add("style", "display:block");
        //tdMessageGreen.InnerHtml = "Subscription Emails sent successfully.";
    }
}